// components/date-bar/index.js
import Toast from '../../lib/vant/toast/toast'       
Component({
  /**
   * 组件的属性列表
   */
  properties: {
  
  },

  attached: function () {
    var date = new Date();//获取系统当前时间
    var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
    var month= date.getMonth()+1; //获取当前月份(0-11,0代表1月)
    var formatMonth = month>9?month:"0"+month;
    var day = date.getDate(); //获取当前日(1-31)
    var formatDay =  day>9?day:"0"+day;
    this.setData({
      starttime: year+"-"+formatMonth+"-01" ,
      endtime: year+"-"+formatMonth+"-"+formatDay
    });
   },
   
  /** 
   * 组件的初始数据
   */
  data: {
    starttime: '',
    endtime: ''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onStartTimeChange:function(e){
      //console.log(e);
      this.setData({
        starttime:e.detail.value
      });
      
    },
    onEndTimeChange:function(e){
      //console.log(e);
      this.setData({
        endtime:e.detail.value
      });
     
    },
    onSearch:function(e){
      console.log(e);
      this.triggerEventByName('handleSearch',e);
    },
    /**
     * 根据方法名调用页面方法
     */
    triggerEventByName:function(methodName,e){
      var starttime = this.data.starttime;
      var endtime = this.data.endtime;
      var event = e;
			/*
			 *这里的handleSearch必须和页面的<searchBar bind:handleSearch="..."></searchBar>的绑定的函数名一致
			 *不必与·handleSearch22·一致即可
			 */
      // if(endtime<starttime){
      //   Toast('结束时间必须大于开始时间!');
      //   return;
      // }
			this.triggerEvent(methodName, {
				starttime,endtime,
				event
			}, {})

    }
  }
})
